﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate.Criterion;
using ZDSoft.QLMS.Entity;
using ZDSoft.QLMS.Component;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class QuestionExamController : Controller
    {
        //
        // GET: /QuestionExam/
        public ActionResult Index()
        {
            return View();
        }
        QuestionComponent questionComponent = new QuestionComponent();
        TypeComponent typeComponent = new TypeComponent();
        public string GetPaged(int pageIndex, int pageSize, string strSearchProperty,
string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Question> tasks = new Question().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
            string jsonStr = "[";

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Question entity in tasks)
            {
                jsonStr += "{\"ID\":" + pageNum + ",\"Title\":\"" + entity.Title + "\",\"Degree\":\"" + entity.Degree + "\",\"IsActive\":\"" + entity.IsActive + "\"},";

            }
            jsonStr += "{\"ID\":" + pageNum + "}";
            jsonStr += "]";
            return jsonStr;
        }
        public ActionResult Update(int id)
        {  
            Question question = questionComponent.FindByID(id);
            TempData["auditOpinion"] = question.AuditOpinion;
            return View(question);
        }
        public void UpdateWithdraw(int id, bool activeinfo, string AuditOpinion)
        {   
            Question question = questionComponent.FindByID(id);
            question.IsActive = activeinfo;
            question.AuditOpinion = AuditOpinion;
            question.Update();
        }

    }
}
